#include "types.h"
#include "user.h"

int semId;


void run(){
	//while(1){
		binary_sem_down(semId);
		printf(1, "------------------------------------\n");
		printf(1,"Process %d Thread %d is running1.\n", thread_getProcId(),thread_getid());
		printf(1,"Process %d Thread %d is running2.\n", thread_getProcId(),thread_getid());
		printf(1,"Process %d Thread %d is running3.\n", thread_getProcId(),thread_getid());
		binary_sem_up(semId);
		sleep(1);
	//}
	thread_exit(0);
}


int main(int argc, char* argv[]){
	semId = binary_sem_create();
	void** ret_val= 0;
	int num = atoi(argv[1]);
	int i;
	int last_thread = -2;
	for (i = 0; i < num; i++){
		int* stack = malloc(4096);
		last_thread = thread_create((void(*))run, stack, 4096);
	}
	thread_join(last_thread, ret_val);
	exit();
	return 0;
}

